package shop.yaojianfeng.springlearning.section4.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import shop.yaojianfeng.springlearning.section4.entity.Product;
import shop.yaojianfeng.springlearning.section4.service.ProductService;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 产品 控制器
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    @Qualifier("productService")
    private ProductService service;

    @RequestMapping("/addProduct")
    private String addProduct(Product product, MultipartFile imgFile, MultipartFile useFile, HttpServletRequest request) throws IOException {
        //生成id
        String pid = UUID.randomUUID().toString();
        product.setPid(pid);
        //存储图片并设置产品img的路径
        String imgName = saveFile(imgFile, pid, request);
        product.setImg("Jeff/fileDir/"+imgName);

        //存储使用说明书并设置其路径
        String useName = saveFile(useFile, pid, request);
        product.setDescription("Jeff/fileDir/"+useName);

        //存储数据库
        service.insert(product);

        //给页面绑定数据
        request.setAttribute("product",product);
        return "productInfo";
    }


    /**
     * 文件下载
     * @param fileName
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/downLoad")
    @ResponseBody
    public ResponseEntity download(String fileName,HttpServletRequest request) throws IOException {
        //获取文件目录
        String realPath = request.getServletContext().getRealPath("Jeff/fileDir/");
        //获取文件名字
        String[] split = fileName.split("/");
        fileName = split[split.length - 1];
        //生成字节流
        String filePath = realPath+fileName;
        byte[] bytes = FileUtils.readFileToByteArray(new File(filePath));
        //设置响应头
        MultiValueMap<String, String> headers = new HttpHeaders();
        //设置保存的文件名
        headers.add("Content-Disposition","attachment;filename="+fileName);
        //设置文件类型
        headers.add("Content-Type","application/xxx");

        //封装响应体
        return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
    }


    /**
     * 存储文件的自定义方法
     * @param file
     * @param pid
     * @param request
     * @return
     * @throws IOException
     */
    private String saveFile(MultipartFile file,String pid,HttpServletRequest request) throws IOException {
        //截取文件后缀名,生成新的文件名字
        String filename = file.getOriginalFilename();
        String extendName = filename.substring(filename.lastIndexOf("."));
        String fileSaveName= pid+"_"+System.currentTimeMillis()+extendName;
        //获取img目录在服务器的路径
        String fileSavePath = request.getServletContext().getRealPath("Jeff/fileDir");
        //构建存储路径
        String savePath =fileSavePath+"/"+fileSaveName;
        //存储文件
        file.transferTo(new File(savePath));

        return fileSaveName;
    }



}
